package com.runnor.common.websys.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runnor.common.websys.system.entity.Sysdict;
import com.runnor.common.websys.system.entity.bo.SysDictBo;
import com.runnor.common.websys.system.mapper.SysdictMapper;
import com.runnor.common.websys.system.service.ISysdictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author runnor-yh
 * @date 2023年08月15日 9:57
 */
@Service
public class SysdictServiceImpl extends ServiceImpl<SysdictMapper, Sysdict> implements ISysdictService {

    @Autowired
    SysdictMapper sysdictMapper;

    @Override
    public Page pages(Page page, QueryWrapper<SysDictBo> queryWrapper) {
        return sysdictMapper.pages(page, queryWrapper);
    }

    @Override
    public List<Sysdict> getSysDictListTree() {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("enable", 1);

        List<Sysdict> list = sysdictMapper.selectList(queryWrapper);

        List build = TreeUtil.build(list, null, TreeNodeConfig.DEFAULT_CONFIG, (sysdict, tree) -> {
            tree.setName(sysdict.getName());
            tree.setId(sysdict.getCode());
            tree.setParentId(sysdict.getParentCode());
            tree.setWeight(sysdict.getSort());
        });

        return build;
    }
}
